package com.jiankang.askhealth.core.transmitter.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.jiankang.askhealth.base.custom.PaginationResult;
import com.jiankang.askhealth.core.transmitter.custom.TransmitterSearchBean;
import com.jiankang.askhealth.core.transmitter.dao.TransmitterMapper;
import com.jiankang.askhealth.core.transmitter.domain.Transmitter;
import com.jiankang.askhealth.core.transmitter.domain.TransmitterExample;
import com.jiankang.askhealth.core.transmitter.domain.TransmitterExample.Criteria;
import com.jiankang.askhealth.core.transmitter.service.TransmitterService;

public class TransmitterServiceImpl implements TransmitterService {
	private TransmitterMapper transmitterMapper;

	@Override
	public void addTransmitter(Transmitter transmitter) {
    	transmitter.setLoveNum(0);
    	transmitter.setDibblingNum(0L);
    	transmitter.setCommentNum(0);
    	transmitter.setCreateDatetime(new Date());
		transmitterMapper.insert(transmitter);
	}

	@Override
	public void update(Transmitter transmitter) {
		transmitterMapper.updateByPrimaryKeySelective(transmitter);
	}

	@Override
	public Transmitter getByPk(Integer transmitterId) {
		return transmitterMapper.selectByPrimaryKey(transmitterId);
	}

	@Override
	public PaginationResult<Transmitter> getTransmitterList(
			TransmitterSearchBean transmitterSearchBean, int start, int maxSize) {
		PaginationResult<Transmitter> result = new PaginationResult<Transmitter>();
		TransmitterExample example = new TransmitterExample();
		
		if (maxSize > 0) {
			example.setLimitStart(start);
			example.setLimitMaxSize(maxSize);
		}
		
		Criteria criteria = example.createCriteria();
		if(transmitterSearchBean != null) {
			if(StringUtils.isNotEmpty(transmitterSearchBean.getName())) {
				criteria.andNameLike("%" + transmitterSearchBean.getName() + "%");
			}
		}
		
		List<Transmitter> transmitters = transmitterMapper.selectByExample(example);
		int total = transmitterMapper.countByExample(example);
		result.setList(transmitters);
		result.setTotal(total);
		
		return result;
	}

	public void setTransmitterMapper(TransmitterMapper transmitterMapper) {
		this.transmitterMapper = transmitterMapper;
	}

}
